<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Controller for managing companies.
 * @package cms-companies/conrollers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2014
 */
class Company extends CMS_Controller {

    /**
     *  Constructor method.
     */
    function __construct()
    {
        parent::__construct();
        
        $this->load->model('company_model');
        
        $this->load->helper('cms');
        $this->load->helper('company');
        
        $this->load->library('form_validation');
//        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small class="help-block">', '</small></div>');
        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small>', '</small>');
        
        init_company();
    }

    /**
     * Default method called when nothing is specified.
     * Displays dashboard.
     * @access public
     */
    public function index()
    {
        view_companies_list();
    }
    
    /*
     * Method called for adding a new record
     * @access public
     */
    public function add()
    {
        if( isset($_POST) && !empty($_POST) )
        {
            if ( valid_company('add') )
            {
                $company = array();
                $company['name']             = clean_text( set_value('name') );
                $company['description']      = clean_text( set_value('description') );
                $company['status']           = 'active';
                
                $company_id = $this->company_model->insert($company);

                if( $this->input->post('add-another') )
                {
                    $this->form_validation->unset_field_data();
                    view_company_form('Add Company', 'add_success');
                } else
                {
                    view_companies_list(NULL, 'add_success');
                }
            }
            
        } else
        {
            view_company_form('Add Company');
        }
    }
    
    /**
     * Method called for editing a new record.
     * @access public
     */
    public function edit()
    {
        $params = $this->uri->uri_to_assoc();

        if( isset($_POST) && ! empty($_POST) )
        {
            if ( valid_company('edit') )
            {
                $company = array();
                $company['name']                = clean_text( set_value('name') );
                $company['description']         = clean_text( set_value('description') );
//                $company['status']              = set_value('status');
                $company['date_modified']       = date('Y-n-d H:i:s');

                $this->company_model->update(array('id' =>set_value('id')), $company);
                view_companies_list(NULL, 'add_success');
            }
        } else
        {
            if( isset($params['id']) && valid_get_id($params['id']) )
            {
                $company = $this->company_model->get_by_id($params['id']);
                view_company_form('Edit Company', NULL, $company);
            } else
            {
                $this->show_404();
            }
        }
    }
    
    /**
     * Method called when deleting a record.
     * @access public
     */
    public function delete()
    {
        $params = $this->uri->uri_to_assoc();
        if( isset($params['id']) && valid_get_id($params['id']) )
        {
            // delete from database
            $deleted = $this->company_model->delete(array('id' => $params['id']));
            if($deleted === 'deleted')
            {
                view_companies_list(NULL, 'delete_success');
            } else
            {
                view_companies_list(NULL, 'archived');
            }
        } else
        {
            $this->show_404();
        }
    }
    
    /*
     * Preview information about a record
     * @access public
     */
    public function preview()
    {
        $params = $this->uri->uri_to_assoc();
        if( isset($params['id']) && valid_get_id($params['id']) )
        {
            $company = $this->company_model->get_by_id($params['id']);
            view_company_preview($company);
        } else
        {
            $this->show_404();
        }
    }
    
    /**
     * Method called when deleting multiple companies.
     * @access public
     */
    public function delete_multiple()
    {        
        if(valid_ids())
        {
            $items = $this->input->post('items');
            $archived = $this->company_model->until_it_sleeps($items);
            
            if(count($archived) > 0)
            {
                view_list(NULL, 'archived');
            } else
            {
                view_list(NULL, 'delete_success');
            }
        } else
        {
            $this->show_404();
        }
    }
    
}

/* End of file company.php */
/* Location: ./application/companies/cms-companies/controllers/company.php */